package main

import (
	"fmt"
	mqtt "github.com/eclipse/paho.mqtt.golang"
	"os"
)

func main() {
	opts := mqtt.NewClientOptions().
		AddBroker("tcp://120.76.117.54:1883").
		SetUsername("xcpt").SetPassword("xcpt2021")
	client := mqtt.NewClient(opts)

	if token := client.Connect(); token.Wait() && token.Error() != nil {
		fmt.Println(token.Error())
		os.Exit(1)
	}

	// 订阅
	for {
		topic := "xcpt_topic/WaterPra/+"
		client.Subscribe(topic, 0, func(client mqtt.Client, msg mqtt.Message) {
			fmt.Printf("Received message: %s from topic: %s\n", msg.Payload(), msg.Topic())
		})
		//client.Disconnect(250)
		//fmt.Println("Subscription ended.")
	}

	// 发布
	//topic := "home/bedroom/temperature"
	//message := "25°C"
	//token := client.Publish(topic, 0, false, message)
	//token.Wait()
	//client.Disconnect(250)
	//fmt.Println("Message published.")
}
